import validHtml from 'ngtemplate!html!../../../tpls/dialog/valid.dialog.html';

class ChipListController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, ChipService, tableService, iRunner){

    	var vm = this;

    	vm.$rootScope    = $rootScope;
    	vm.$cookieStore  = $cookieStore;
    	vm.$timeout 	 = $timeout;
    	vm.$state   	 = $state;
    	vm.ngDialog 	 = ngDialog;
    	vm.dialogService = dialogService;
    	vm.ChipService   = ChipService;
    	vm.tableService  = tableService;
    	vm.iRunner 	     = iRunner;

    	// 拼接接口数据
        var params = {
            action: 'chip',
            name: 'dashboard.race.chipedit',
            id: 'id'
        }

        // 初始化表格控件, ajax请求数据
        vm.tableService.getChipData(params, vm.$cookieStore.get('access_token'), vm.$state.params.race_id);
        

        // 清空芯片信息
        vm.deleteAll = function() {

            // 模态框显示文本
            $rootScope.common = {
                text : '如果存在已绑定选手的芯片，删除芯片信息，与芯片绑定的选手将会解绑。'
            }

            vm.ngDialog.open({
                template: validHtml,
                className: 'ngdialog-theme-default ngdialog-alert',
                scope: $scope
            });
        }

        // 清空芯片信息, 表单提交
        $rootScope.submitDelForm = function(isValid) {

            // 验证失败, 提示用户
            if (!isValid) {
                vm.dialogService.error();
            }

            // 验证通过
            if (isValid) {

                // 关闭模态框
                ngDialog.close();

                // 清空芯片
                vm.ChipService.postChipClear({}, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
                .then(function(data) {
                    
                    // 成功调用
                    data.code==0&&vm.dialogService.success('已清空芯片信息');

                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);

                    console.log(data)
                }, function(data) {
                    console.log('error')
                });
            }

            return false;
        }

    }
}

ChipListController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'ChipService', 'tableService', 'iRunner']; 

export default ChipListController;